<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\PriceLog;
use support\Request;
use support\Response;
use plugin\admin\app\model\Order;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 订单列表 
 */
class OrderController extends Crud
{
    
    /**
     * @var Order
     */
    protected $model = null;

    protected $goods_id = 0;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Order;
    }

    public function select(Request $request): Response
    {
        $params = $request->all();
        $this->goods_id = $params['goods_id'];
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $query = $this->doSelect($where, $field, $order)->where('is_pay',1);
        return $this->doFormat($query, $format, $limit);
    }

    protected function doFormat($query, $format, $limit): Response
    {
        $methods = [
            'select' => 'formatSelect',
            'tree' => 'formatTree',
            'table_tree' => 'formatTableTree',
            'normal' => 'formatNormal',
        ];
        $paginator = $query->paginate($limit);
        $total = $paginator->total();
        $items = $paginator->items();

        // 计算资金池金额
        $total_price = PriceLog::where('goods_id',$this->goods_id)->value('price');
        $total_price = priceFormat($total_price);
        $reward_order = Order::where(['is_reward'=>0])->orderBy('id')->whereNotNull('reward_total_price')->first();
        foreach ($items as $k => $v){
            $price = priceFormat($v['reward_total_price']);
            if (!empty($reward_order) && $v['id']==$reward_order['id']){
                $items[$k]['progress'] = bcdiv($total_price,$price, 2)*100 . '%';
            }
            if ($v['is_reward'] == 1){
                $items[$k]['progress'] = '100%';
            }
        }
        if (method_exists($this, "afterQuery")) {
            $items = call_user_func([$this, "afterQuery"], $items);
        }
        $format_function = $methods[$format] ?? 'formatNormal';
        return call_user_func([$this, $format_function], $items, $total,['total_price'=>$total_price]);
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('order/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('order/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('order/update');
    }

}
